package com.fly.control;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.fly.conn.DBConnect;
import com.fly.model.Cashier;

public class CashierControl {
	public static boolean insertCashier(Cashier c){
		String sql = "insert into cashier(singlePrice,stoId saler, nums, time, buyer) values(?, ?, ?, ?, ?, ?)";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setInt(i++, c.getSinglePrice());
			dbc.setInt(i++, c.getStockId());
			dbc.setInt(i++, c.getSaler());
			dbc.setInt(i++, c.getNums());
			dbc.setString(i++, c.getTime());
			dbc.setInt(i++, c.getBuyer());
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用InsertCashier函数失败");
		} finally{
			dbc.close();
		} 
		return false;
	}
	public static boolean deleteCashier(int id){
		String sql = "delete * from cashier where id = ?";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用deleteCashier函数失败");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean updateCashier(Cashier c,int id){
		String sql = "update cashier set singlePrice = ?, stoId = ?, saler = ?, nums = ?, time = ?, buyer = ? where id  = " + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setInt(i++, c.getSinglePrice());
			dbc.setInt(i++, c.getStockId());
			dbc.setInt(i++, c.getSaler());
			dbc.setInt(i++, c.getNums());
			dbc.setString(i++, c.getTime());
			dbc.setInt(i++, c.getBuyer());
			dbc.executeUpdate();
			return true;
		} catch (Exception e){
			e.printStackTrace();
			System.out.println("updateCashier ERROR!");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static List<Cashier> getCashierByTime(String startTime,String endTime){
		String sql = "select * from cashier where time between '" +startTime + "' and '" + endTime + "'";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		List<Cashier> list = new ArrayList<Cashier>();
		ResultSet rs = null;
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				Cashier c = new Cashier();
				c.setId(rs.getInt("id"));
				c.setStockId(rs.getInt("stoId"));
				c.setSinglePrice(rs.getInt("singlePrice"));
				c.setSaler(rs.getInt("saler"));
				c.setNums(rs.getInt("nums"));
				c.setTime(rs.getString("time"));
				c.setBuyer(rs.getInt("buyer"));
				list.add(c);
			}
			return list;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用getCashierByTime函数失败");
		} finally{
			dbc.close();
		}
		return null;
	} 
}
